using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using DNAide.Core;
using DNAide.Web.UI.Scripts;
using DNAide.Web.UI.Styles;

namespace DNAide.Website.Templates.MasterPages
{
	public partial class Default : System.Web.UI.MasterPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			// Page title
			Page.Title = string.Format("DNAide v{0} - ", ConfigurationManager.AppSettings["DNAideVersion"]);
			if (!string.IsNullOrEmpty(SiteMap.CurrentNode["longTitle"]))
			{
				this.Header1.Text = SiteMap.CurrentNode["longTitle"];
			}
			else
			{
				this.Header1.Text = SiteMap.CurrentNode.Title;
			}
			Page.Title += SiteMap.CurrentNode.Title;

			// Register styles
			StyleAide.RegisterInclude("~/Client/Styles/Master.css");
			if (!string.IsNullOrEmpty(SiteMap.CurrentNode["colorCSS"]))
			{
				string colorCss = string.Format("{0}.css", SiteMap.CurrentNode["colorCSS"]);
				StyleAide.RegisterInclude(string.Format("~/Client/Styles/{0}", colorCss));	
			}
			//StyleAide.CurrentContext.RegisterInclude("IE.css", "~/Client/Styles/IE.css", "screen", "lte IE 6");
			//StyleAide.CurrentContext.RegisterInclude("IE7.css", "~/Client/Styles/IE7.css", "screen", "IE 7");

			// Register scripts
			ScriptAide.RegisterDNAideResource(DNAideScriptResource.JQuery);
			ScriptAide.RegisterInclude("~/Client/Scripts/Global.js");

			// Build top nav
			StringWriter sw = new StringWriter();
			HtmlTextWriter writer = new HtmlTextWriter(sw);
			writer.WriteFullBeginTag("ul");
			writer.WriteFullBeginTag("li");
			writer.WriteBeginTag("a");
			writer.WriteAttribute("href", SiteMap.RootNode.Url);
			if (SiteMap.CurrentNode.Key == SiteMap.RootNode.Key)
			{
				writer.WriteAttribute("class", "active");
			}
			writer.Write(HtmlTextWriter.TagRightChar);
			writer.Write(SiteMap.RootNode.Title);
			writer.WriteEndTag("a");
			writer.WriteEndTag("li");
			foreach (SiteMapNode childNode in SiteMap.RootNode.ChildNodes)
			{
				if (!CastAide.AsBoolean(childNode["ignoreInNavigationPrimary"], false))
				{
					writer.WriteFullBeginTag("li");
					writer.WriteBeginTag("a");
					writer.WriteAttribute("href", childNode.Url);
					if (SiteMap.CurrentNode.Key == childNode.Key || SiteMap.CurrentNode.IsDescendantOf(childNode))
					{
						writer.WriteAttribute("class", "active");
					}
					writer.Write(HtmlTextWriter.TagRightChar);
					writer.Write(childNode.Title);
					writer.WriteEndTag("a");
					writer.WriteEndTag("li");
				}
			}
			writer.WriteEndTag("ul");

			this.NavigationPrimaryHtml.Text = sw.GetStringBuilder().ToString();

			// Tracking Code
			if (CastAide.AsBoolean(ConfigurationManager.AppSettings["ShowTrackingCode"]))
			{
				this.TrackingCode.Visible = true;
			}

		}
	}
}
